package com.tanhua.dubbo.server.api;


import com.alibaba.dubbo.config.annotation.Service;
import com.tanhua.dubbo.server.pojo.UserLocation;
import com.tanhua.dubbo.server.vo.UserLocationVo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service(version = "1.0.0")
public class UserLocationApiImpl implements UserLocationApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public String updateUserLocation(Long userId, Double longitude, Double latitude, String address) {
        UserLocation userLocation = new UserLocation();
        userLocation.setAddress(address);
        userLocation.setUserId(userId);
        //输入经纬,设置坐标
        userLocation.setLocation(new GeoJsonPoint(longitude,latitude));
        //查看用户的地理位置
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation ul = this.mongoTemplate.findOne(query, UserLocation.class);
        if (null==ul){
            //新增用户的位置数据
            userLocation.setId(ObjectId.get());
            //拿到更新时间
            userLocation.setCreated(System.currentTimeMillis());
            //设置更最近一次更新时间
            userLocation.setUpdated(userLocation.getCreated());
            userLocation.setLastUpdated(System.currentTimeMillis());
            this.mongoTemplate.save(userLocation);

            return userLocation.getId().toHexString();

        }else {
            //更新(地理位置更新)
            Update update = Update.update("location",userLocation.getLocation())
                    .set("update",System.currentTimeMillis())
                    .set("lastUpdated",ul.getLocation())
                    .set("address",userLocation.getAddress());
            this.mongoTemplate.updateFirst(query,update, UserLocation.class);
        }
        //更新返回
        return ul.getId().toHexString();
    }

    /**
     * 根据userId查询位置数据
     * @param userId
     * @return
     */
    @Override
    public UserLocationVo queryByUserId(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId));
        UserLocation userLocation = this.mongoTemplate.findOne(query, UserLocation.class);
        if (null != userLocation){
            //调用vo format 内的方法
            return UserLocationVo.format(userLocation);
        }
        return null;
    }

    /**
     * 指定范围查询
     * @param longitude
     * @param latitude
     * @param  range 范围 单位 米
     * @return
     */
    @Override
    public List<UserLocationVo> queryUserFromLocation(Double longitude, Double latitude, Integer range) {
        //根据传入的坐标,进行确定中心点
        GeoJsonPoint geoJsonPoint = new GeoJsonPoint(longitude,latitude);
        //设置半径,默认值为米  除以1000变成千米
        Distance distance = new Distance(range / 1000, Metrics.KILOMETERS);

        //画一个中心圈
        Circle circle = new Circle(geoJsonPoint, distance);

        //根据圈查询
        Query query = Query.query(Criteria.where("location").withinSphere(circle));
        List<UserLocation> userLocations = this.mongoTemplate.find(query, UserLocation.class);

        return UserLocationVo.formatToList(userLocations);
    }
}
